package cn.gdcp.lkqapp08;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ThreeLevelCity extends AppCompatActivity {
    private String TAG = "ThreeLevelCity";
    private Spinner spinner1 = null;
    private Spinner spinner2 = null;
    private Spinner spinner3 = null;
    private String provinceName;
    private String cityName;
    private String areaName;
    private Button btnSave;
    private JSONArray provinceJsonArray;
    private JSONArray cityJsonArray;
    private JSONArray areaJsonArray;
    private JSONObject provinceObject;
    private JSONObject cityJsonObject;
    private String[] province;
    private String[] city;
    private String[] area;
    private ArrayAdapter<String> provinceArrayAdapter = null;
    private ArrayAdapter<String> cityArrayAdapter =null;
    private ArrayAdapter<String> areaArrayAdapter =null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three_level_city);

        run1();
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        provinceArrayAdapter = new ArrayAdapter<String>(ThreeLevelCity.this, R.layout.support_simple_spinner_dropdown_item, province);
        spinner1.setAdapter(provinceArrayAdapter);
        spinner1.setSelection(2, true);

        run2();

        spinner2 = (Spinner) findViewById(R.id.spinner2);
        cityArrayAdapter = new ArrayAdapter<String>(ThreeLevelCity.this, R.layout.support_simple_spinner_dropdown_item, city);
        spinner2.setAdapter(cityArrayAdapter);
        spinner2.setSelection(2,true);

        run3();

        spinner3 = (Spinner) findViewById(R.id.spinner3);
        areaArrayAdapter = new ArrayAdapter<String>(ThreeLevelCity.this, R.layout.support_simple_spinner_dropdown_item, area);
        spinner3.setAdapter(areaArrayAdapter);
        spinner3.setSelection(2,true);


        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                try {
                    for (int i = 0; i < provinceJsonArray.length(); i++) {
                        provinceObject = provinceJsonArray.getJSONObject(i);
                        provinceName = provinceObject.getString("name");
                        if (provinceName.equals(province[position])) {
                            cityJsonArray = provinceObject.getJSONArray("city");
                            city = new String[cityJsonArray.length()];
                            for (int j = 0; j < cityJsonArray.length(); j++) {
                                cityJsonObject = cityJsonArray.getJSONObject(j);
                                cityName = cityJsonObject.getString("name");
                                city[j] = cityName;
                            }
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                cityArrayAdapter = new ArrayAdapter<String>(ThreeLevelCity.this, R.layout.support_simple_spinner_dropdown_item, city);
                spinner2.setAdapter(cityArrayAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                try {
                    for (int i = 0; i < cityJsonArray.length(); i++) {
                        cityJsonObject = cityJsonArray.getJSONObject(i);
                        cityName = cityJsonObject.getString("name");
                        if (cityName.equals(city[position])){
                            areaJsonArray = cityJsonObject.getJSONArray("area");
                            area = new String[areaJsonArray.length()];
                            for (int j = 0; j < areaJsonArray.length(); j++) {
                                areaName = areaJsonArray.getString(j);
                                area[j] = areaName;
                            }
                        }
                    }
                }catch (JSONException e) {
                    e.printStackTrace();
                }

                areaArrayAdapter = new ArrayAdapter<String>(ThreeLevelCity.this, R.layout.support_simple_spinner_dropdown_item, area);
                spinner3.setAdapter(areaArrayAdapter);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        btnSave = findViewById(R.id.btn_save);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append(spinner1.getSelectedItem());
                stringBuffer.append(spinner2.getSelectedItem());
                stringBuffer.append(spinner3.getSelectedItem());
                Log.e(TAG,"所选城市地区：" + stringBuffer);

                Intent intent = new Intent();
                intent.putExtra("cityarea",stringBuffer.toString());
                setResult(2001,intent);
                finish();
            }
        });


    }



    private void run1() {
        try {
            InputStream inputStream = getResources().getAssets().open("data.txt");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "stringBuffer = " + stringBuffer);

            provinceJsonArray = new JSONArray(stringBuffer.toString());
            province = new String[provinceJsonArray.length()];
            for (int i = 0; i < provinceJsonArray.length(); i++) {
                provinceObject = provinceJsonArray.getJSONObject(i);
                provinceName = provinceObject.getString("name");
                province[i] = provinceName;
            }
            Log.e(TAG, "province = " + province);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    private void run2() {
        try {
            for (int i = 0; i < provinceJsonArray.length(); i++) {
                provinceObject = provinceJsonArray.getJSONObject(i);
                provinceName = provinceObject.getString("name");
                if (provinceName.equals(province[2])) {
                    cityJsonArray = provinceObject.getJSONArray("city");
                    city = new String[cityJsonArray.length()];
                    for (int j = 0; j < cityJsonArray.length(); j++) {
                        cityJsonObject = cityJsonArray.getJSONObject(j);
                        cityName = cityJsonObject.getString("name");
                        city[j] = cityName;
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void run3() {
        try {
            for (int i = 0; i < cityJsonArray.length(); i++) {
                cityJsonObject = cityJsonArray.getJSONObject(i);
                cityName = cityJsonObject.getString("name");
                if (cityName.equals(city[2])){
                    areaJsonArray = cityJsonObject.getJSONArray("area");
                    area = new String[areaJsonArray.length()];
                    for (int j = 0; j < areaJsonArray.length(); j++) {
                        areaName = areaJsonArray.getString(j);
                        area[j] = areaName;
                    }
                }
            }
        }catch (JSONException e) {
            e.printStackTrace();
        }
    }
}